﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLL.Entities;
using DLL.Entities.Event;
using DLL.Entities.Status;
using DLL.Entities.Config;
using System.IO;

namespace COM
{
    public partial class frmCom : Form
    {
        #region Declarations
        ReadXML xRead = null;
        WriteXML xWrite = null;
        COMConfig configC = null;
        
        RADStatus statusR = null;
        MISStatus statusM = null;
        DateTime oldRADStatus = DateTime.Today.AddDays(-1);
        DateTime oldMISStatus = DateTime.Today.AddDays(-1);

        RADEvent eventR = null;
        //MISEvent eventM = null;
        #endregion

        public frmCom()
        {
            InitializeComponent();

            xRead = new ReadXML();
            xWrite = new WriteXML();
            configC = xRead.ReadCOMConfig();
        }

        private void frmCom_Load(object sender, EventArgs e)
        {
            tRead.Enabled = true;
            lblVersionCom.Text = configC.Version.ToString();
        }

        #region Events
        public void tRead_Tick(object sender, EventArgs e)
        {
            // Status Reading
            ReadStatus();

            // Event Reading
            ReadEvent();
        }

        private void btnFire_Click(object sender, EventArgs e)
        {
            xWrite.WriteCOMEvent(1, Convert.ToInt16(lblAltX.Text), Convert.ToInt16(lblSpdX.Text), Convert.ToInt16(lblDistX.Text), 1, configC.COMEventDir);
            lblMisFired.Text = "Missile Fired";
            lblMisFired.ForeColor = Color.ForestGreen;
            btnAbort.Enabled = true;

            txtbxAAR.Text = "DETECTED AT:\t" + eventR.DetectionTime.ToString() + Environment.NewLine +
                                    "ALTITUDE:\t" + eventR.Altitude.ToString() + Environment.NewLine +
                                    "SPEED:\t\t" + eventR.Speed.ToString() + Environment.NewLine +
                                    "DISTANCE:\t" + eventR.Distance.ToString() + Environment.NewLine +
                                    "POSITION:\t" + eventR.Position.ToString() + Environment.NewLine +
                                    "HEADING:\t" + eventR.Heading.ToString() + Environment.NewLine + Environment.NewLine +
                                    "FIRED AT:\t" + DateTime.Now;
                btnFire.Enabled = false;        
        }

        private void btnAbort_Click(object sender, EventArgs e)
        {
            xWrite.WriteCOMEvent(0, 0, 0, 0, 2, configC.COMEventDir);
            btnAbort.Enabled = false;
            lblMisFired.Text = "Missile Aborted";
            lblMisFired.ForeColor = Color.DarkRed;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            StreamWriter sw;
            using (sw = File.AppendText("MissileLaunch.txt"))
            {
                sw.WriteLine(Environment.NewLine);
                sw.WriteLine(txtbxAAR.Text);
            }
            //StreamWriter sw = new StreamWriter("MissileLaunch.txt");
            //sw.WriteLine(txtbxAAR.Text);
            sw.Close();
        }
        #endregion

        #region Custom Methods
        private void ReadStatus()
        {
            statusR = xRead.ReadRADStatus(configC.RADStatusDir);

            statusM = xRead.ReadMISStatus(configC.MISStatusDir);

            if (statusR.CurrentStatus.Equals(oldRADStatus))
            {
                lblRadOnOff.Text = "Offline";
                lblRadOnOff.ForeColor = Color.DarkRed;
            }
            else
            {
                lblRadOnOff.Text = "Online";
                lblRadOnOff.ForeColor = Color.ForestGreen;
                oldRADStatus = statusR.CurrentStatus;
            }

            if (statusM.CurrentStatus.Equals(oldMISStatus))
            {
                lblMisOnOff.Text = "Offline";
                lblMisOnOff.ForeColor = Color.DarkRed;
            }
            else
            {
                lblMisOnOff.Text = "Online";
                lblMisOnOff.ForeColor = Color.ForestGreen;
                oldMISStatus = statusM.CurrentStatus;
            }

            lblMisCntX.Text = statusM.MissileCount.ToString();
        }

        private void ReadEvent()
        {
            try
            {
                eventR = xRead.ReadRADEvent(configC.RADEventDir);

                if (eventR.Speed.Equals(0))
                {
                    lblAltX.Text = "";
                    lblSpdX.Text = "";
                    lblDistX.Text = "";
                    lblPosX.Text = "";
                    lblHeadX.Text = "";
                    lblRadDetect.Text = "Clear Sky";
                    lblRadDetect.ForeColor = Color.ForestGreen;
                    btnFire.Enabled = false;
                }
                else
                {
                    lblAltX.Text = eventR.Altitude.ToString();
                    lblSpdX.Text = eventR.Speed.ToString();
                    lblDistX.Text = eventR.Distance.ToString();
                    lblPosX.Text = eventR.Position.ToString();
                    lblHeadX.Text = eventR.Heading;
                    lblRadDetect.Text = "Bogie Detected";
                    lblRadDetect.ForeColor = Color.DarkRed;
                    btnFire.Enabled = true;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        #endregion
    }
}
